#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

enum ink 
{
	black = 0x00, 
	darkblue = 0x01, 
	darkgreen = 0x02, 
	darkcyan = 0x03,
	darkred = 0x04, 
	purple = 0x05, 
	darkyellow = 0x06, 
	lightgrey = 0x07, 
	grey = 0x08,
	blue = 0x09, 
	green = 0x0A, 
	cyan = 0x0B, 
	red = 0x0C, 
	magenta = 0x0D, 
	yellow = 0x0E, 
	white = 0x0F
};

std::wostream &operator << (std::wostream &colorize, enum ink color)
{
	HANDLE hConsoleOutput;
	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsoleOutput, color);
	return colorize;
}

struct setc
{
	setc (enum ink fg, enum ink bg) : fg (fg), bg (bg) {}
	enum ink fg;
	enum ink bg;
};

std::wostream &operator << (std::wostream &colorize, setc &color)
{
	HANDLE hConsoleOutput;
	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsoleOutput, (color.fg|color.bg*0x10));
	return colorize;
}

struct setattr
{
	setattr(WORD attr) : attr(attr) {};
	WORD attr;
};

std::wostream &operator << (std::wostream &colorize, setattr &attr)
{
	HANDLE hConsoleOutput;
	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsoleOutput, attr.attr);
	return colorize;
}

void ReadWordList()
{
}

int main(int argc, char**argv)
{
	HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO bi;
	GetConsoleScreenBufferInfo(hConsoleOutput, &bi);
	
	int color = -1;
	if (argc > 1)
	{
		color = atoi(argv[1]);
	}

	wchar_t line[1024];
	while (!wcin.eof())
	{
		wcin.getline(line, 1024);
		if (0 == wcslen(line))
		{
			continue;
		}
		
		if (color >= 0)
		{
			wcout << (ink)color;
		}
		
		if (wcsstr(line, L"error ") 
			|| wcsstr(line, L"error:") 
			|| wcsstr(line, L"Merge Module with key")
			|| wcsstr(line, L"Error")
			|| wcsstr(line, L"...skipped")
			|| wcsstr(line, L"...failed")
		)
		{
			wcout << red;
		}
		else if (wcsstr(line, L"warning ") || wcsstr(line, L"warning:"))
		{
			wcout << darkgreen;
		}


		wcout << line << setattr(bi.wAttributes) << endl;
	}

	SetConsoleTextAttribute(hConsoleOutput, bi.wAttributes);

	return 0;
}
